package main;

import java.util.ArrayList;
import java.util.HashMap;

import common.Constants;

import util.FetchUtil;
import crawler.CrawlerDocument;
import crawler.XmlExtractor;

public class XmlRunner {

	public static void main(String[] args) {
		try {
			HashMap<String, String> xpaths = new HashMap<String, String>();
			xpaths.put("link", "loc/#text");
			xpaths.put("title", "data/display/title/#cdata-section/");
			xpaths.put("image", "data/display/small_image/#text");
			xpaths.put("start_time", "data/display/startTime/#text");
			xpaths.put("end_time", "data/display/endTime/#text");
			xpaths.put("ori_price", "data/display/value/#text");
			xpaths.put("cur_price", "data/display/price/#text");
			xpaths.put("rebate", "data/display/rebate/#text");
			xpaths.put("bought", "data/display/bought/#text");
			xpaths.put("detail", "data/display/detail/#cdata-section/");
			
			// TODO read seed from a file 
			CrawlerDocument doc = new CrawlerDocument("http://open.client.lashou.com/api/detail/city/2419/p/1/r/10");
			
			doc.addFeature("xpath_root", "/urlset/url");
					
			doc.addFeature(Constants.DOC_FEATURE_KEY_XPATHS, xpaths);
			
			FetchUtil.fetch(doc);	
			
			XmlExtractor extractor = new XmlExtractor();
			
			extractor.Extract(doc);
			
			ArrayList<CrawlerDocument> outlinks = doc.getOutlinks();
			
			System.out.println("size = " + outlinks.size());
						
			for(int i = 0; i < outlinks.size(); ++i){
				CrawlerDocument extDoc = outlinks.get(i);				
				System.out.println("link = "+ extDoc.getFeature("link"));
				System.out.println("image = " + extDoc.getFeature("image"));
				System.out.println("text = " + extDoc.getFeature("title"));
				System.out.println("ori_price = " + extDoc.getFeature("ori_price"));
				System.out.println("cur_price = " + extDoc.getFeature("cur_price"));
				System.out.println("----------------------------------------");				
			}	
					
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
